#include "HUD.h"

	HUD::HUD(int x, int y, SDL_Surface *spritesheet): Sprite(x, y, 0, 0, spritesheet)
	{
		energy = 50;
		health = 10;
		reserve = 0;
		ammo = 1;
		maxreserve = 0;
		maxammo = 1;

		reservesprite.x = 0;
		reservesprite.y = 60;
		reservesprite.w = 30;
		reservesprite.h = 30;

		gaugesprite.x = 0;
		gaugesprite.y = 227;
		gaugesprite.w = 52;
		gaugesprite.h = 4;

		ammogauge.x = x+47;
		ammogauge.y = y+16;
		ammogauge.w = ammo*10;
		ammogauge.h = 2;

		energygauge.x = x+45;
		energygauge.y = y+21;
		energygauge.w = energy;
		energygauge.h = 2;

		healthbar.x = 0;
		healthbar.y = 213;
		healthbar.w = 49;
		healthbar.h = 14;

		HUDsheetloc.x = 0;
		HUDsheetloc.y = 90;
		HUDsheetloc.w = 99;
		HUDsheetloc.h = 40;
	}

	bool HUD::Update(int hp, int eng, bool reserveup, int ammomod, bool ammoup)
	{
		//Modifiers
		health += hp;
		energy += eng;
		ammo += ammomod;

		//Energy Logic
		if(energy < 0)
			energy = 0;
		if(energy > 50)
			energy = 50;

		//Ammo Logic
		if(ammo > maxammo)
			ammo = maxammo;
		if(ammo < 0)
			ammo = 0;
		if(ammoup == true && maxammo < 5)
		{
			maxammo++;
			ammo += 1;
		}

		//Health and Reserve Logic
		if(health == 0)
		{
			reserve--;
			health = 10;
			healthbar.w = 49;
		}

		if(health > 10 && reserve < maxreserve)
		{
			health = 1;
			reserve++;
		}
		else if(health > 10 && reserve == maxreserve)
			health = 10;

		if(reserveup == true && maxreserve < 5)
		{
			maxreserve++;
			reserve++;
			health = 10;
		}

		if(reserve > maxreserve)
			reserve = maxreserve;


		//Graphical updates
		reservesprite.x = reserve*30;
		healthbar.w = (health*5)-1;
		energygauge.w = energy;
		ammogauge.w = ammo*10;

		//Game over check
		if(reserve == -1)
		{
			return true;
		}

		return false;
	}

	void HUD::Render(void(*RenderFunction)(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip), SDL_Surface* destination)
	{

		//HUD Overlay
		RenderFunction(x, y, sprite, destination, &HUDsheetloc);
		//Reserve
		RenderFunction(x+5, y+5, sprite, destination, &reservesprite);
		//Health Bar
		RenderFunction(x+41, y+25, sprite, destination, &healthbar);
		//Ammo Gauge
		RenderFunction(x+46, y+15, sprite, destination, &gaugesprite);
		ammogauge.x = x + 47;
		ammogauge.y = y + 16;
		SDL_FillRect(destination, &ammogauge, SDL_MapRGB(destination->format, 220, 200, 0x00));
		//Energy Gauge
		RenderFunction(x+44, y+20, sprite, destination, &gaugesprite);
		energygauge.x = x + 45;
		energygauge.y = y + 21;
		SDL_FillRect(destination, &energygauge, SDL_MapRGB(destination->format, 0x00, 0x00, 255));

	}